*************************************
*** Panel Analysis: GLES *************
*************************************
clear
global OUT "SET_YOUR_OUTPATH"

// this is for data which is later read into R
global OUT2 "SET_YOUR_OUTPATH_FOR_DATAFRAMES"	

*create dummy datasets for regsave

*hybrid models
clear
input coef stderr ci_lower ci_upper N _id
. . . . . .
end
save "$OUT2\GLES\gles_coef_hhinc.dta", replace

clear
input coef stderr ci_lower ci_upper N _id
. . . . . .
end
save "$OUT2\GLES\gles_coef_hhinc_dec_pp_sqrt.dta", replace

clear
input coef stderr ci_lower ci_upper N _id
. . . . . .
end
save "$OUT2\GLES\gles_coef_loginc.dta", replace

clear
input coef stderr ci_lower ci_upper N _id
. . . . . .
end
save "$OUT2\GLES\gles_coef_hhinc_sat.dta", replace


*bivariate
clear
input coef stderr ci_lower ci_upper N _id
. . . . . .
end
save "$OUT2\GLES\gles_coef_hhinc_0.dta", replace

clear
input coef stderr ci_lower ci_upper N _id
. . . . . .
end
save "$OUT2\GLES\gles_coef_hhinc_dec_pp_sqrt_0.dta", replace

clear
input coef stderr ci_lower ci_upper N _id
. . . . . .
end
save "$OUT2\GLES\gles_coef_loginc_0.dta", replace

clear
input coef stderr ci_lower ci_upper N _id
. . . . . .
end
save "$OUT2\GLES\gles_coef_hhinc_sat_0.dta", replace


*equivalence tests
clear
input coef stderr ci_lower ci_upper N _id
. . . . . .
end
save "$OUT2\GLES\gles_coef_hhinc_dec_pp_sqrt_e.dta", replace



*load data
use "SET_YOUR_INPATH\GLES\wkp09-17_long.dta", clear

*relevant variables:
*sep: inc (net hh inc, month) ewl (sat. w. pers. econ. sit.) fear_jobloss (only 2013&2017)
*pol. vars: 
	*interest: polint
	*vote: itv (intention to vote) imp_elec (imp. elections), imp_elec_camp, 
	*effint1 effint2 (int. eff.), effex1 effex2 (ext. eff.), vote_diff
	*inet_pol print_pol* tv_pol* wissen_verb wissen_vis
*socdem: hhsize (num. pers. in hh), age, sex, unemployed, nlf, edu
*ids: id year 


xtset id year, delta(4)


*** recode variables

//use hhinc with standardized hhinc
egen zhhinc = std(hhinc)
replace hhinc = zhhinc

// use income deciles from 1-10
rename hhinc_dec10 hhinc_dec10_
clonevar hhinc_dec10 = hhinc_dec 
 
rename hhinc_dec_pp10 hhinc_dec_pp10_
clonevar hhinc_dec_pp10 = hhinc_dec_pp 
 
rename hhinc_dec_pp_sqrt10 hhinc_dec_pp_sqrt10_
clonevar hhinc_dec_pp_sqrt10 = hhinc_dec_pp_sqrt 


// gen log income
gen loginc = ln(hhinc_pp_sqrt)

*create interaction variables
gen hhinc0Xage = hhinc * age18
gen hhinc0_ppXage = hhinc_pp * age18
gen hhinc0_pp_sqrtXage = hhinc_pp_sqrt * age18

gen hhincXage = hhinc_dec10 * age18
gen hhinc_ppXage = hhinc_dec_pp10 * age18
gen hhinc_pp_sqrtXage = hhinc_dec_pp_sqrt10 * age18
gen ewlXage = ewl10 * age18

gen hhincXage1835 = hhinc_dec10 * age1835
gen ewlXage1835 = ewl10 * age1835


* generate income change/jobloss variables
gen incdiff = hhinc_dec-L.hhinc_dec 

recode incdiff (-9/-1=1 "shock")(0/9=0 "no shock")(else=.), gen(incdiff1)
recode incdiff (-9/-2=1 "shock")(-1 0/9=0 "no shock")(else=.), gen(incdiff2)


*create engagement scales via factor analysis
reg vote polint10 itv10 pidstr10 effint10 imp_elec10 imp_elec_camp10  ///
		media_pol10 wissen_verb10 wissen_vis10
gen mysample1 = e(sample)
polychoric vote polint10 itv10 pidstr10 effint10 imp_elec10 imp_elec_camp10  ///
		media_pol10 wissen_verb10 wissen_vis10 if mysample1==1

global N = r(sum_w)
matrix r = r(R)
factormat r, n($N) factors(1)
predict f1
sum f1
gen engagement = ((f1 - r(min)) / (r(max)-r(min)))*10




/*
* summary stats for within and between variation
// change
tsspell vote, end(end_vote) seq(seq_vote) spell(spell_vote) cond(vote >= 0 & vote < .)
egen n_spell_vote = max(spell_vote), by(id)

tsspell incdiff2, end(end_incdiff) seq(seq_incdiff) spell(spell_incdiff) cond(incdiff2 >= 0 & incdiff2 < .)
egen n_spell_incdiff = max(spell_incdiff), by(id)

// gen sample
reg hhinc_dec_pp_sqrt10 female age18 unemployed nlf edu_med edu_high migrant id
gen mysample = e(sample)

xtsum hhinc_dec_pp_sqrt10 polint vote n_spell_vote incdiff2 n_spell_incdiff if mysample==1
*/

* labels
lab var polint10 "Pol. Interest"
lab var partyid "Party ID"
lab var pidstr10 "PID Strength"
lab var itv10 "Intention to Vote"
lab var imp_elec10 "Imp. Elections"
lab var imp_elec_camp10 "Imp. Elec. Camp."
lab var media_pol10 "Pol. Media Use"
lab var wissen_verb10 "Pol. Know.: Verbal"
lab var wissen_vis10 "Pol. Know.: Visual"
lab var effint10 "Int. Efficacy"
lab var vote "Vote"
lab var engagement "Political Engagement"

lab var hhinc "Income"
lab var hhinc_dec_pp_sqrt10 "Income"
lab var ewl10 "Sat. Income"

lab var sex "Sex"
lab var age18 "Age"
lab var unemployed "Unemployed"
lab var nlf "Not in Labor Force"
lab var edu_med "Edu: medium"
lab var edu_high "Edu: high"


**************************************
***** Analysis ***********************
**************************************

/* lagged variables
gen hhinc_dec1 = L.hhinc_dec
gen hhinc_sat1 = L.hhinc_sat
gen polint1 = L.polint
gen vote1 = L.vote
*/

estpost summarize polint10 partyid pidstr10 itv10 effint10 imp_elec10 imp_elec_camp10  ///
			media_pol10 wissen_verb10 wissen_vis10 ///
			hhinc hhinc_pp hhinc_pp_sqrt hhinc_dec10 hhinc_dec_pp10 hhinc_dec_pp_sqrt10 ewl10 incdiff2 sex age18 age1835 unemployed nlf edu_med edu_high ///
			hhincXage ewlXage hhincXage1835 hhinc_ppXage hhinc_pp_sqrtXage ewlXage1835 
esttab using "$OUT\Tables\GLES\summary_stats_gles.rtf", cells("count mean(fmt(3)) sd(fmt(3))  min max") replace


* regressions with age (continuous)

// for coef export
foreach var of varlist vote polint10 itv10 partyid pidstr10 effint10 imp_elec10 imp_elec_camp10  ///
		media_pol10 wissen_verb10 wissen_vis10 {
	xthybrid `var' hhinc, clusterid(id) full
	eststo m001_`var'
	regsave W__hhinc B__hhinc using "$OUT2\GLES\gles_coef_hhinc_0.dta", ci level(95) autoid append
	xthybrid `var' hhinc sex age18 unemployed nlf edu_med edu_high, clusterid(id) full
	eststo m01_`var'
	regsave W__hhinc B__hhinc using "$OUT2\GLES\gles_coef_hhinc.dta", ci level(95) autoid append
	
	xthybrid `var' loginc, clusterid(id) full
	eststo m002_`var'
	regsave W__loginc B__loginc using "$OUT2\GLES\gles_coef_loginc_0.dta", ci level(95) autoid append
	xthybrid `var' loginc sex age18 unemployed nlf edu_med edu_high, clusterid(id) full
	eststo m02_`var'
	regsave W__loginc B__loginc using "$OUT2\GLES\gles_coef_loginc.dta", ci level(95) autoid append
	
	
	xthybrid `var' hhinc_dec_pp_sqrt10, clusterid(id) full
	eststo m03_`var'
	regsave W__hhinc_dec_pp_sqrt10 B__hhinc_dec_pp_sqrt10 using "$OUT2\GLES\gles_coef_hhinc_dec_pp_sqrt_0.dta", ci level(95) autoid append
	xthybrid `var' hhinc_dec_pp_sqrt10 sex age18 unemployed nlf edu_med edu_high, clusterid(id) full
	eststo m3_`var'
	regsave W__hhinc_dec_pp_sqrt10 B__hhinc_dec_pp_sqrt10 using "$OUT2\GLES\gles_coef_hhinc_dec_pp_sqrt.dta", ci level(95) autoid append
	
	xthybrid `var' ewl10, clusterid(id) full
	eststo m04_`var'
	regsave W__ewl10 B__ewl10 using "$OUT2\GLES\gles_coef_hhinc_sat_0.dta", ci level(95) autoid append
	xthybrid `var' ewl10 sex age18 unemployed nlf edu_med edu_high , clusterid(id) full
	eststo m4_`var'
	regsave W__ewl10 B__ewl10 using "$OUT2\GLES\gles_coef_hhinc_sat.dta", ci level(95) autoid append
}

// for equivalence testing
foreach var of varlist  vote polint10 itv10 partyid pidstr10 effint10 imp_elec10 imp_elec_camp10  ///
		media_pol10 wissen_verb10 wissen_vis10 {
	xtreg `var' hhinc_dec_pp_sqrt10 age18 unemployed nlf, fe
	regsave hhinc_dec_pp_sqrt10 using "$OUT2\GLES\gles_coef_hhinc_dec_pp_sqrt_e.dta", ci level(90) autoid append
}


// interactions
foreach var of varlist vote polint10 itv10 partyid pidstr10 effint10 imp_elec10 imp_elec_camp10  ///
		media_pol10 wissen_verb10 wissen_vis10 {
	xthybrid `var' hhinc_dec_pp_sqrt10 sex age18 unemployed nlf edu_med edu_high hhinc_pp_sqrtXage, clusterid(id) full
	eststo m7_`var'
}



// test: objective income on subjective income
foreach var of varlist hhinc  hhinc_dec_pp_sqrt10 incdiff2 {
	xthybrid ewl10 `var' female age18 unemployed nlf edu_med edu_high migrant , clusterid(id) full
	eststo inc_so_`var'
}
esttab inc_so_hhinc inc_so_hhinc_dec_pp_sqrt10 inc_so_incdiff2 ///
	using "$OUT\Tables\GLES\hybrid_all_inc_subj_obj.rtf", b(2) se replace
esttab inc_so_hhinc inc_so_hhinc_dec_pp_sqrt10 inc_so_incdiff2 ///
	using "$OUT\Tables\GLES\hybrid_all_inc_subj_obj.tex", b(2) se replace

	

// two tables
esttab m3_vote m7_vote m3_polint10 m7_polint10 m3_itv10 m7_itv10 m3_partyid m7_partyid m3_pidstr10 m7_pidstr10 m3_effint10 m7_effint10 ///
	using "$OUT\Tables\GLES\hybrid_all_inc_pp_sqrt_1.rtf", b(3) se replace	
esttab m3_imp_elec10 m7_imp_elec10 m3_imp_elec_camp10 m7_imp_elec_camp10 ///
		m3_media_pol10 m7_media_pol10 m3_wissen_verb10 m7_wissen_verb10 m3_wissen_vis10 m7_wissen_vis10 ///
	using "$OUT\Tables\GLES\hybrid_all_inc_pp_sqrt_2.rtf", b(3) se replace	
esttab m3_vote m7_vote m3_polint10 m7_polint10 m3_itv10 m7_itv10 m3_partyid m7_partyid m3_pidstr10 m7_pidstr10 m3_effint10 m7_effint10 ///
	using "$OUT\Tables\GLES\hybrid_all_inc_pp_sqrt_1.tex", b(3) se replace label scalars("N N") nonumbers booktabs alignment(D{.}{.}{3})	///
	varlabels ///
				(R__sex "Sex" ///
				R__edu_med "Edu: medium" ///
				R__edu_high "Edu: high" ///
				W__hhinc "W: Income" ///
				W__age18 "W: Age" ///
				W__unemployed "W: Unemployed" ///
				W__nlf "W: Not in Labor Force" ///
				B__hhinc "B: Income" ///
				B__age18 "B: Age" ///
				B__unemployed "B: Unemployed" ///
				B__nlf "B: Not in Labor Force" ///
				W__hhinc "W: Income" ///
				B__hhinc "B: Income" ///
				W__hhinc_dec_pp_sqrt10 "W: Income (dec.)" ///
				B__hhinc_dec_pp_sqrt10 "B: Income (dec.)" ///
				W__ewl10 "W: Sat. Income" ///
				B__ewl10 "B: Sat. Income")
				
esttab m3_imp_elec10 m7_imp_elec10 m3_imp_elec_camp10 m7_imp_elec_camp10 ///
		m3_media_pol10 m7_media_pol10 m3_wissen_verb10 m7_wissen_verb10 m3_wissen_vis10 m7_wissen_vis10 ///
	using "$OUT\Tables\GLES\hybrid_all_inc_pp_sqrt_2.tex", b(3) se replace label scalars("N N") nonumbers booktabs alignment(D{.}{.}{3})	///
	varlabels ///
				(R__sex "Sex" ///
				R__edu_med "Edu: medium" ///
				R__edu_high "Edu: high" ///
				W__hhinc "W: Income" ///
				W__age18 "W: Age" ///
				W__unemployed "W: Unemployed" ///
				W__nlf "W: Not in Labor Force" ///
				B__hhinc "B: Income" ///
				B__age18 "B: Age" ///
				B__unemployed "B: Unemployed" ///
				B__nlf "B: Not in Labor Force" ///
				W__hhinc "W: Income" ///
				B__hhinc "B: Income" ///
				W__hhinc_dec_pp_sqrt10 "W: Income (dec.)" ///
				B__hhinc_dec_pp_sqrt10 "B: Income (dec.)" ///
				W__ewl10 "W: Sat. Income" ///
				B__ewl10 "B: Sat. Income" ///
				W_hhinc_dec_pp_sqrtXage "W: Inc. X Age" ///
				B_hhinc_dec_pp_sqrtXage "B: Inc. X Age")


				

// engagement scale:
xthybrid engagement hhinc_dec_pp_sqrt10 sex age18 unemployed nlf edu_med edu_high, clusterid(id) full
eststo m_engage	
	
esttab m_engage ///
	using "$OUT\Tables\GLES\hybrid_engage_eqinc.tex", b(3) se replace label nonumbers booktabs alignment(D{.}{.}{3}) scalars("N N")	///
	varlabels ///
				(R__sex "Sex" ///
				R__migrant "Migrant" ///
				R__edu_med "Edu: medium" ///
				R__edu_high "Edu: high" ///
				W__edu_med "Edu: medium" ///
				W__edu_high "Edu: high" ///
				B__edu_med "Edu: medium" ///
				B__edu_high "Edu: high" ///
				W__age18 "W: Age" ///
				B__age18 "B: Age" ///
				W__unemployed "W: Unemployed" ///
				B__unemployed "B: Unemployed" ///
				W__nlf "W: Not in Labor Force" ///
				B__nlf "B: Not in Labor Force" ///
				W__hhinc_dec_pp_sqrt10 "W: Income (dec.)" ///
				B__hhinc_dec_pp_sqrt10 "B: Income (dec.)" ///
				_cons "Constant")
		
						
				
				
	

*logistic model for vote and partyid
set seed 1234

foreach var of varlist vote partyid {	
	xthybrid `var' hhinc_dec_pp_sqrt10 sex age18 unemployed nlf edu_med edu_high, clusterid(id) family(binomial) link(logit) meglmopts(iterate(200) intpoints(3)) full iterations
	eststo m5_`var'_logit
}
esttab m5_vote_logit m5_partyid_logit   ///
	using "$OUT\Tables\GLES\hybrid_logit.rtf", b(3) se replace
esttab m5_vote_logit m5_partyid_logit  ///
	using "$OUT\Tables\GLES\hybrid_logit.tex", b(3) se replace label nonumbers scalars("N N") booktabs alignment(D{.}{.}{3})	///
	varlabels ///
				(R__sex "Female" ///
				R__edu_med "Edu: medium" ///
				R__edu_high "Edu: high" ///
				W__hhinc "W: Income" ///
				W__age18 "W: Age" ///
				W__unemployed "W: Unemployed" ///
				W__nlf "W: Not in Labor Force" ///
				B__hhinc "B: Income" ///
				B__age18 "B: Age" ///
				B__unemployed "B: Unemployed" ///
				B__nlf "B: Not in Labor Force" ///
				W__hhinc "W: Income" ///
				B__hhinc "B: Income" ///
				W__hhinc_dec_pp_sqrt10 "W: Income (dec.)" ///
				B__hhinc_dec_pp_sqrt10 "B: Income (dec.)" ///
				W__ewl10 "W: Sat. Income" ///
				B__ewl10 "B: Sat. Income")

